package com.smartfactory.sail.controller;


import com.ada.base.common.ApiResponse;
import com.ada.base.common.ControllerResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.smartfactory.sail.config.HeaderWrapper;
import com.smartfactory.sail.constant.header.PickingListEnum;
import com.smartfactory.sail.entity.Picking;
import com.smartfactory.sail.service.IPickingService;
import com.smartfactory.sail.vo.PageParam;
import com.smartfactory.sail.vo.picking.PickInfoResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 王巨壁
 * @since 2019-08-26
 */
@RestController
@RequestMapping("/picking")
@Api(tags = "领料管理")
public class PickingController extends ControllerResponse {

    private IPickingService iPickingService;

    public PickingController(IPickingService iPickingService) {
        this.iPickingService = iPickingService;
    }

    @HeaderWrapper(PickingListEnum.class)
    @ApiOperation(value = "领料管理列表", notes = "领料管理列表")
    @PostMapping("pickingList/{currentPage}/{size}/{sortType}/{sortField}")
    public ApiResponse<IPage<Picking>> pickingList(@RequestBody Map<String, Object> queryParam,
                                                   @PathVariable("currentPage") Long currentPage,
                                                   @PathVariable("size") Integer size,
                                                   @PathVariable("sortType") Integer sortType,
                                                   @PathVariable("sortField") String sortField) {

        PageParam pageParam = new PageParam(currentPage, size, sortType, sortField);
        return ok(iPickingService.listPageByWhere(pageParam, queryParam));
    }

    @ApiOperation(value = "获取领料详情",notes = "根据orderItemId获取详情")
    @GetMapping("pickingInfo/{id}")
    public ApiResponse<PickInfoResp> pickingInfo(@PathVariable("id")Long id) {
        return ok(iPickingService.getPickingInfoByOrderItemId(id));
    }

    @ApiOperation(value = "提交领料信息",notes = "提交领料信息")
    @PostMapping("commit/{recordId}")
    public ApiResponse<Object> commit(@PathVariable("recordId") Long recordId) {
        try {
            return ok(iPickingService.commit(recordId));
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }

    @GetMapping("test")
    public ApiResponse<Object> test() {
        iPickingService.creatPicking();
        return ok();
    }
}
